package com.zpyr.mvc.common;

import java.util.ArrayList;
import java.util.List;

import com.zpyr.mvc.vo.AddrList;

public class AddrTree {
	
	private List<AddrList> addr_list;
	private List<String> sido_list;
	private List<String> gubun_list;
	private List<String> dong_list;
	
	private boolean sidoFirst , gubunFirst;
	private int lastAdded;
	
	public AddrTree( List<AddrList> addr_list ) {
		
		sidoFirst = true;
		gubunFirst = true;
		
		this.addr_list = addr_list;
		sido_list  = new ArrayList<String>();
		gubun_list  = new ArrayList<String>();
		dong_list  = new ArrayList<String>();
		
		lastAdded = 0; // 1 : sido , 2 : gubun , 3 : dong
		
	}
	
	private String close() {
		return "]},";
	}
	private String close2() {
		return "]}";
	}
	
	private String addDong( String dong ) {
		String s = "{";
		if(lastAdded == 3) s = ", {";
		lastAdded = 3;
		return s + "text : '" + dong + "' , leaf : true }";
	}

	private String addGubun( String gubun ) {
		String ret = "";
		ret += "{ text : '" + gubun + "'";
		ret += ", items : [";
		lastAdded = 2;
		return ret;
	}
		
	private String addSido( String sido ) {
		String ret = "";
		ret += "{ text : '" + sido + "'";
		ret += ", items : [";
		lastAdded = 1;
		return ret; 
	}
		
	private String addDong2(String sido , String gubun , String dong) {
		String ret = "";
		if( !dong_list.contains( sido+gubun+dong ) ) {
			// 처음 검색된 dong 값
			dong_list.add( sido+gubun+dong );
			ret = addDong( dong );
		}
		else {
			// sido , gubun , dong 전부 처음이 아니다. doNothong
		}
		return ret;
	}
	
	private String addGubun2(String sido , String gubun , String dong){
		String ret = "";
		if( !gubun_list.contains( sido+gubun ) ) {
			// 처음 검색된 gubun 값
			if(!gubunFirst) {
				if(lastAdded == 1);
				else if(lastAdded == 2) ret += close();
				else if(lastAdded == 3) ret += close();
			}
			gubunFirst = false;
			gubun_list.add( sido+gubun );
			ret += addGubun( gubun );
			
			ret += addDong2(sido , gubun , dong);
			
		}
		else {
			// sido , gubun 값이 이미 있다.
			ret += addDong2(sido , gubun , dong);
		}		
		return ret;
	}
	
	public String getTreeData() {
		String ret = "{ text : '전국' , items : [";
		String sido , gubun , dong;
		
		for( AddrList addr : addr_list ) {
			sido = addr.getAddr_sido();
			gubun = addr.getAddr_gubun();
			dong = addr.getAddr_dong();
			
			if( !sido_list.contains(  sido ) ){
				// 처음 검색된 sido 값
				if(!sidoFirst) {
					if(lastAdded == 1) ret += close();
					if(lastAdded == 2) ret += close2() + close();
					else if(lastAdded == 3) ret += close2() + close();
				}
				sidoFirst = false;
				sido_list.add( sido );
				ret += addSido( sido );
				gubunFirst = true;
				ret += addGubun2( sido , gubun , dong );
			}
			else {
				// sido 값이 똑같다.
				ret += addGubun2( sido , gubun , dong );
			}
					
		}
		String last = "";
		if(lastAdded == 3) last = "]} ]} ]}";
		else if(lastAdded == 2) last = "]} ]} ]}";
		else if(lastAdded == 1) last = "]} ]}";
		return ret + last;
	}
	
}
